/**
 * Created by Administrator on 2017/6/30 0030.
 */
"use strict";
angular.module('app.yksensor').controller('YKAlertCtrl', function ($scope,$http) {

    //配置分页基本参数
    $scope.paginationConf = {
        currentPage: 1,
        itemsPerPage: 10,
        pagesLength: 5,
        totalItems: 20,
        perPageOptions: [10, 20, 30, 40, 50],
        onChange: function () {
        }
    };


    $scope.sends = function () {

        // console.log($('#startdate').val());

        $scope.paginationConf.deviceid = $scope.paginationConf.deviceid == undefined ? "" : $scope.paginationConf.deviceid;    
        $scope.paginationConf.handle_status = $scope.paginationConf.handle_status == undefined ? "" : $scope.paginationConf.handle_status;
        var timefrom = $('#startdate').val();
        var timeto = $('#finishdate').val();
        if(timefrom == "" && timeto == ""){
            $scope.paginationConf.time = "";
        }else{
            $scope.paginationConf.time = $('#startdate').val()+","+$('#finishdate').val();
        }

        // console.log($scope.paginationConf);
        // $http.get(baseurls+"/list_nopage/warn_report?deviceid="+$scope.paginationConf.deviceid+"&handle_status="+$scope.paginationConf.handle_status+"&time="+$scope.paginationConf.time).then(function(res){
        //     console.log(res.data);
        //     if(res.data.ret == 1){
        //         $scope.datas = res.data.info;
        //         $scope.paginationConf.totalItems = res.data.info.length;
        //     }
        // })
        $http.post(baseurls+"/list/select/warn_report?deviceid="+$scope.paginationConf.deviceid+"&handle_status="+$scope.paginationConf.handle_status+"&time="+$scope.paginationConf.time, $scope.paginationConf).then(function(res){
            // console.log(res.data)
            if(res.data.ret == 1){
                $scope.datas = res.data.rows;
                $scope.paginationConf.totalItems = res.data.records;
            }
            
        })
        
    }
    $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage',$scope.sends);

    $scope.editdata = {};
    $scope.showEditAlert = function (data) {
        $scope.editdata.id = data.id;
        $scope.editdata.deviceid = data.deviceid;
        $scope.editdata.memo =  data.memo;

        $('#editAlert').modal('show');
    }

    $scope.updatedata = function () {

        if ($scope.editdata.memo)
            $scope.editdata.handle_status = "1";
        else
            $scope.editdata.handle_status = "0";

        // console.log($scope.editdata);
        $http.put(baseurls+"/data/warn_report?id=="+$scope.editdata.id, $scope.editdata).then(function(res){
            if(res.data.ret == 1){
                //更新设备表 status 设备状态为正常   暂时这样吧  可是没效果啊
                console.log($scope.editdata.deviceid)
                $http.put(baseurls+"/data/device?id=="+$scope.editdata.deviceid, {status: "1"}).then(function(res1){
                    console.log(res1.data)
                    if(res1.data.ret ==1){
                        new Notify('success','处理成功');
                    }
                })
            }else if(res.data.ret != -4){
                new Notify('fail','处理失败');
            }
        })
        $('#editAlert').modal('hide');
        $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage',$scope.sends);//？？不能刷新？

    }
});